package kr.hkit.myframework;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import kr.hkit.myframework.db.DBProperties;

public class FactoryDBProperties extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private JTextField tfFilePath;
	private JTextField tfDriver;
	private JTextField tfUrl;
	private JTextField tfUser;
	private JTextField tfPassword;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FactoryDBProperties frame = new FactoryDBProperties();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public FactoryDBProperties() {
		initialize();
	}
	
	private void initialize() {
		setTitle("데이터베이스 연결 설정 파일 생성");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 388, 265);
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel pCenter = new JPanel();
		contentPane.add(pCenter, BorderLayout.CENTER);
		GridBagLayout gbl_pCenter = new GridBagLayout();
		gbl_pCenter.columnWidths = new int[]{0, 84, 235, 0};
		gbl_pCenter.rowHeights = new int[]{35, 35, 35, 35, 35, 0};
		gbl_pCenter.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_pCenter.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		pCenter.setLayout(gbl_pCenter);
		
		JLabel lblfileName = new JLabel("파일명");
		lblfileName.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblfileName = new GridBagConstraints();
		gbc_lblfileName.insets = new Insets(0, 0, 5, 5);
		gbc_lblfileName.anchor = GridBagConstraints.EAST;
		gbc_lblfileName.gridx = 1;
		gbc_lblfileName.gridy = 0;
		pCenter.add(lblfileName, gbc_lblfileName);
		
		tfFilePath = new JTextField();
		tfFilePath.setText("dbcon.xml");
		tfFilePath.setToolTipText("예)dbcon.xml");
		GridBagConstraints gbc_tfFilePath = new GridBagConstraints();
		gbc_tfFilePath.insets = new Insets(0, 0, 5, 0);
		gbc_tfFilePath.fill = GridBagConstraints.BOTH;
		gbc_tfFilePath.gridx = 2;
		gbc_tfFilePath.gridy = 0;
		pCenter.add(tfFilePath, gbc_tfFilePath);
		tfFilePath.setColumns(10);
		
		JLabel lblDriver = new JLabel("Driver");
		lblDriver.setHorizontalAlignment(SwingConstants.TRAILING);
		GridBagConstraints gbc_lblDriver = new GridBagConstraints();
		gbc_lblDriver.anchor = GridBagConstraints.EAST;
		gbc_lblDriver.insets = new Insets(0, 0, 5, 5);
		gbc_lblDriver.gridx = 1;
		gbc_lblDriver.gridy = 1;
		pCenter.add(lblDriver, gbc_lblDriver);
		
		tfDriver = new JTextField();
		tfDriver.setText("com.mysql.jdbc.Driver");
		tfDriver.setToolTipText("ex)com.mysql.jdbc.Driver");
		GridBagConstraints gbc_tfDriver = new GridBagConstraints();
		gbc_tfDriver.insets = new Insets(0, 0, 5, 0);
		gbc_tfDriver.fill = GridBagConstraints.BOTH;
		gbc_tfDriver.gridx = 2;
		gbc_tfDriver.gridy = 1;
		pCenter.add(tfDriver, gbc_tfDriver);
		tfDriver.setColumns(10);
		
		JLabel lblUrl = new JLabel("URL");
		GridBagConstraints gbc_lblUrl = new GridBagConstraints();
		gbc_lblUrl.anchor = GridBagConstraints.EAST;
		gbc_lblUrl.insets = new Insets(0, 0, 5, 5);
		gbc_lblUrl.gridx = 1;
		gbc_lblUrl.gridy = 2;
		pCenter.add(lblUrl, gbc_lblUrl);
		
		tfUrl = new JTextField();
		tfUrl.setText("jdbc:mysql://localhost:3306/erp");
		tfUrl.setToolTipText("ex) jdbc:mysql://localhost:3306/데이터베이스명");
		GridBagConstraints gbc_tfUrl = new GridBagConstraints();
		gbc_tfUrl.insets = new Insets(0, 0, 5, 0);
		gbc_tfUrl.fill = GridBagConstraints.BOTH;
		gbc_tfUrl.gridx = 2;
		gbc_tfUrl.gridy = 2;
		pCenter.add(tfUrl, gbc_tfUrl);
		tfUrl.setColumns(10);
		
		JLabel lblUser = new JLabel("User");
		GridBagConstraints gbc_lblUser = new GridBagConstraints();
		gbc_lblUser.anchor = GridBagConstraints.EAST;
		gbc_lblUser.insets = new Insets(0, 0, 5, 5);
		gbc_lblUser.gridx = 1;
		gbc_lblUser.gridy = 3;
		pCenter.add(lblUser, gbc_lblUser);
		
		tfUser = new JTextField();
		GridBagConstraints gbc_tfUser = new GridBagConstraints();
		gbc_tfUser.insets = new Insets(0, 0, 5, 0);
		gbc_tfUser.fill = GridBagConstraints.BOTH;
		gbc_tfUser.gridx = 2;
		gbc_tfUser.gridy = 3;
		pCenter.add(tfUser, gbc_tfUser);
		tfUser.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password");
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(0, 0, 0, 5);
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = 4;
		pCenter.add(lblPassword, gbc_lblPassword);
		
		tfPassword = new JTextField();
		GridBagConstraints gbc_tfPassword = new GridBagConstraints();
		gbc_tfPassword.fill = GridBagConstraints.BOTH;
		gbc_tfPassword.gridx = 2;
		gbc_tfPassword.gridy = 4;
		pCenter.add(tfPassword, gbc_tfPassword);
		tfPassword.setColumns(10);
		
		JPanel pBottom = new JPanel();
		contentPane.add(pBottom, BorderLayout.SOUTH);
		
		JButton btnCreate = new JButton("만들기");
		btnCreate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DBProperties.savePropertis(
						tfFilePath.getText().trim(), 
						tfDriver.getText().trim(), 
						tfUrl.getText().trim(), 
						tfUser.getText().trim(), 
						tfPassword.getText().trim()
				);
				JOptionPane.showMessageDialog(FactoryDBProperties.this, "설정 파일을 생성하였습니다");
				clearTf();				
			}
		});
		
		JButton  btnCancel = new JButton("취   소");
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				clearTf();
			}
		});
		
		pBottom.add(btnCreate);
		pBottom.add(btnCancel);
	}

	
	private void clearTf() {
		tfFilePath.setText("");
		tfDriver.setText("");
		tfUrl.setText("");
		tfUser.setText("");
		tfPassword.setText("");
	}
}
